package org.springboot.sample.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springboot.sample.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public List<User> getUsers(){
		String sql = "select * from tbt_test";
		return jdbcTemplate.query(sql, new RowMapper<User>(){

			@Override
			public User mapRow(ResultSet result, int i) throws SQLException {
				User u = new User();
				u.setId(result.getString("id"));
				u.setFirstName(result.getString("first_name"));
				u.setLastName(result.getString("last_name"));
				/*
				 * 注意：
				 * 如果不转成java.lang.Date，User对象返回json时不会按格式处理，而是直接返回long time格式。
				 */
				u.setDateUpdated(new Date(result.getDate("date_updated").getTime()));
				u.setBirthDay(new Date(result.getDate("birthday").getTime()));
				return u;
			}});
	}
	public List<User> getTmp(){
		List<User> l = new LinkedList<User>();
		User u1 = new User();
		u1.setId("1");
		u1.setDateUpdated(new Date());
		u1.setBirthDay(new java.sql.Date(1350576000000L));
		
		l.add(u1);
		return l;
	}
}

